package wiki.douzi.helpyougrow.service.impl;

import wiki.douzi.helpyougrow.domain.Cart;
import wiki.douzi.helpyougrow.domain.Foods;
import wiki.douzi.helpyougrow.domain.Goods;
import wiki.douzi.helpyougrow.domain.GoodsOrder;
import wiki.douzi.helpyougrow.mapper.CartMapper;
import wiki.douzi.helpyougrow.mapper.FoodsMapper;
import wiki.douzi.helpyougrow.mapper.GoodsMapper;
import wiki.douzi.helpyougrow.mapper.GoodsOrderMapper;
import wiki.douzi.helpyougrow.service.CartService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

/**
 * (Cart)表服务实现类
 *
 * @author Douzi
 * @since 2022-06-18 16:39:23
 */
@Service("cartService")
public class CartServiceImpl implements CartService {
    @Resource
    private CartMapper cartMapper;
    @Resource
    private GoodsOrderMapper goodsOrderMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private FoodsMapper foodsMapper;

    /**
     * 查询所有数据
     *
     * @return 实例对象集合
     */
    @Override
    public List<Cart> queryAll() {
        return this.cartMapper.queryAll();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Cart queryById(Integer id) {
        return this.cartMapper.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param cart 实例对象
     * @return 实例对象
     */
    @Override
    public Cart insert(Cart cart) {
        this.cartMapper.insert(cart);
        return cart;
    }

    /**
     * 修改数据
     *
     * @param cart 实例对象
     * @return 实例对象
     */
    @Override
    public Cart update(Cart cart) {
        this.cartMapper.update(cart);
        return this.queryById(cart.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.cartMapper.deleteById(id) > 0;
    }

    @Override
    public List<Map<String, Object>> checkCartOrder() {
        return this.cartMapper.checkCartOrder();
    }

    @Override
    public int settlementCart(Integer userId, List<Integer> cartList) {
        int sum = 0;
        for (Integer integer : cartList) {
            //通过购物车id找到当前购物出
            Cart cart = this.queryById(integer);
            Integer number = cart.getNumber();
            GoodsOrder goodsOrder = new GoodsOrder();
            goodsOrder.setTCartId(integer);
            int price = 0;
            //找食品
            if (cart.getTGoodsId() == null || cart.getTGoodsId() == -1){
                Integer tFoodsId = cart.getTFoodsId();
                Foods foods = foodsMapper.queryById(tFoodsId);
                price = Integer.parseInt(foods.getCashPrice().toString())*number;
            }
            //找商品
            if (cart.getTFoodsId() == null || cart.getTFoodsId() == -1){
                Integer tGoodsId = cart.getTGoodsId();
                Goods goods = goodsMapper.queryById(tGoodsId);
                price = Integer.parseInt(goods.getCashPrice().toString())*number;
            }
            goodsOrder.setCashPrice(price);
            goodsOrder.setPointsPrice(-1);
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            goodsOrder.setTreatTime(dateFormat.format(date));
            sum += this.goodsOrderMapper.insert(goodsOrder);
            cart.setIsSettle(1);
            sum += this.cartMapper.update(cart);
        }
        return sum;
    }
}

